package com.sh.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.sh.dao.TransportDao;
import com.sh.entity.Transport;
import com.sh.entity.User;
import com.sh.service.TransportService;
import com.sh.util.DateUtil;

@Service()
public class TransportServiceImpl implements TransportService{
	@Autowired
	private TransportDao transportDao;

	/**
	 * 查询
	 * */
	@Override
	public List<Transport> listTransport(Map<String, Object> conMap) {
		// TODO Auto-generated method stub
		return transportDao.listTransport(conMap);
	}

	//上传 excel文件,并且到DB
	@Override
	public String saveUploadTransport(List<Transport> stoneList, User user, String sysTime) {
		// TODO Auto-generated method stub
		int result = transportDao.saveUploadTransport(stoneList,user,sysTime);
		return "";
	}
	/**
	 * 查询分页总数
	 * */
	@Override
	public int listTransportCount(Map<String, Object> conMap) {
		// TODO Auto-generated method stub
		return transportDao.listTransportCount(conMap);
	}
	//删除
	@Override
	public void deleteTransports(String[] tableSelectedIds) {
		// TODO Auto-generated method stub
		transportDao.deleteTransports(tableSelectedIds);
	}
	/**
	 * 新增 / 修改 保存数据
	 * */
	@Override
	public void saveTransports(Transport transport, User user) {
		// TODO Auto-generated method stub
		transportDao.saveTransports(transport,user);
	}

	//判断 数据是不是已经存在
	//根据 【日期】【客户名称】【车牌】【重量】判断
	@Override
	public boolean isTransportExist(Transport transport) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//修改的时候判断数据有么有存在
		if(!StringUtils.isEmpty(transport.getId())){
			int count = transportDao.isTransportExistForUpdate(transport);
			flag = (count==0?false:true);
		}else{
			int count = transportDao.isTransportExistForAdd(transport);
			flag = (count==0?false:true);
		}

		return flag;
	}

	/**
	 * 批量修改 数据
	 * 1、先删除 同个日期，同个客户的所有数据
	 * 2、插入当前数据
	 * */
	@Transactional
	@Override
	public String updateUploadTransport(List<Transport> checkList, User user, String sysTime) {
		// TODO Auto-generated method stub

		//1、先删除 同个日期同个客户的所有数据
		transportDao.deleteTransportsByDateAndCustname(checkList);

		//2、插入当前数据
		transportDao.saveUploadTransportForUpdate(checkList,user,sysTime);

		return "";
	}

	// 根据ID 拿取对应的年份
	@Override
	public List<String> listYearByIds(String[] ids) {
		// TODO Auto-generated method stub 
		List<String> rtnList = new ArrayList<>();
		List list = transportDao.listYearByIds(ids);
		if(list!=null &&
				list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				String transportDate =
						(String)map.get("transport_date");
				if(DateUtil.checkDateFormat(transportDate, "yyyy-MM-dd")){
					if(!rtnList.contains(transportDate.split("-")[0])){
						rtnList.add(transportDate.split("-")[0]);
					}
				}
			}
			return rtnList;

		}
		return null;
	}


}
